import { ReactNode } from 'react';
import { Modal, showModal } from '../../functionalComponent';

export const info = {
  alertTitle: '提示',
  paramError: '参数不正确！',
  selectTemplate: '请选择模板',
  selectOneData: '请至少选择一条数据',
  noCallBack: '未传入callback函数，无法执行回调！',
  templateCode: '编码',
  name: '名称',
  remark: '说明',
  sharePolicy: '共享策略',
  shareTenant: '单元组织',
  group: '集团共享',
  cu: '单元共享',
  org: '组织私有',
  previewType: '预览方式(下拉选择)',
  searchEmptyText: '请输入名称',
  createPrintTitile: '选择打印模板',
  printError: '打印失败'
};

export const commonStyle = {
  borderStyle: {
    borderRadius: 'var(--border-radius-base, 2px)',
    border: '1px solid var(--border-color-base, #f0f0f0)'
  },
  backgroundStyle: {
    backgroundColor: 'var(--component-background, #ffffff)'
  }
};

interface IModal {
  title: string;
  width?: number;
  content: ReactNode;
  okText?: boolean | string;
  cancelText?: boolean | string;
  footerLeft?: ReactNode;
  onOk?: (...args: any[]) => any;
  onCancel?: (...args: any[]) => any;
}

/**
 * 弹窗
 * @param props
 */
export function wfModal(props: IModal) {
  return new Promise((resolve) => {
    const { onOk, onCancel, ...others } = props;
    const oldDestroy = showModal({
      closable: false,
      ...others,
      onOk: async (ins) => {
        ins.destroy = () => {
          resolve(ins.getApi().getOkData ? ins.getApi().getOkData() : true);
          oldDestroy();
        };
        if (onOk) {
          await onOk(ins);
        } else if (ins.getApi().invokeOkHandler) {
          await ins.getApi().invokeOkHandler();
        } else {
          ins.destroy();
        }
      },
      onCancel: async (ins) => {
        ins.destroy = () => {
          resolve(ins.getApi().getCancelData ? ins.getApi().getCancelData() : false);
          oldDestroy();
        };
        if (onCancel) {
          await onCancel(ins);
        } else if (ins.getApi().invokeCancelHandler) {
          await ins.getApi().invokeCancelHandler();
        } else {
          ins.destroy();
        }
      }
    }).destroy;
  });
}

/**
 * 提示框
 * @param title 标题
 * @param content 提示内容
 */
export function wfAlert(title, content) {
  return new Promise((resolve) => {
    Modal.info({
      title: title || info.alertTitle,
      zIndex: 1001,
      content,
      afterClose: () => {
        resolve('close');
      }
    });
  });
}

export enum SharePolicyEnum {
  /**
   * 集团共享
   */
  GROUP = 0,
  /**
   * 单元共享
   */
  CU = 1,
  /**
   * 组织私有
   */
  ORG = 2
}
